package com.chaos.eurekaproducer.leetCode;

import org.apache.commons.lang3.StringUtils;

/**
 * @author liaopeng
 * @title: Offer05
 * @projectName eureka-producer
 * @description:
 * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 * 输入：s = "We are happy."
 * 输出："We%20are%20happy."
 *
 * 思路：
 * 将1个字符替换成了3个字符
 * 用一个3倍长的新数组来接收重新排列的旧数组
 * @date 2021/6/7下午5:26
 */
public class Offer05 {

    public static String replaceSpace(String s){
        char[] chars = s.toCharArray();
        char[] newChars = new char[chars.length*3];
        int j=0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i]==' '){
                newChars[j]='%';
                newChars[j+1]='2';
                newChars[j+2]='0';
                j+=3;
            }else{
                newChars[j]=chars[i];
                j++;
            }
        }
       return new String(newChars,0,j);
    }

    public static void main(String[] args) {
        String s = "We are happy.";
        String s1 = replaceSpace(s);
        System.out.println(s1);
    }
}
